package com.hosever.coreTest;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
	
	public static void main(String[] args) {
		String s = "<token>123123</token>";
		String val = getXMLNodeContentSingle(s, "token");
		System.out.println(val);
	}
	
	/**
	 * 获取xml节点内容
	 * @param resourceString 源字符串
	 * @param nodeName 节点名
	 * @return 值数组
	 */
	public static List<String> getXMLNodeContent(String resourceString,String nodeName){
		List<String> result = new ArrayList<String>();
		//支持大小于号转义字符 20160526 heshihua
		String regex="(<|&lt;|&#60;)"+nodeName+".*?(>|&gt;|&#62;)(.*?)(<|&lt;|&#60;)/"+nodeName+"(>|&gt;|&#62;)";
		Pattern p=Pattern.compile(regex);
		Matcher m=p.matcher(resourceString);
		while(m.find()){
			result.add(m.group(3));
		}
		return result;
	}
	/**
	 * 获取XML（首个）节点内容
	 * @param resourceString 源字符串
	 * @param nodeName 节点名
	 * @return 单个值
	 */
	public static String getXMLNodeContentSingle(String resourceString,String nodeName){
		List<String> resultList = getXMLNodeContent(resourceString, nodeName);
		String result = "";
		if(resultList!=null && !resultList.isEmpty()){
			result = resultList.get(0);
		}
		return result;
	}
}